package com.tiyende.kash.loop.ui.activity

import android.annotation.SuppressLint
import android.view.View
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.common.CommonActivity
import com.tiyende.kash.loop.databinding.TiyendeActivityLimitCompleteBinding
import com.tiyende.kash.loop.event.TiyendeEventData
import com.tiyende.kash.loop.event.TiyendeEventPointType
import com.tiyende.kash.loop.ui.contract.IViewContract
import com.tiyende.kash.loop.ui.presenter.TiyendeLimitPresenter
import com.tiyende.kash.loop.utils.helper.TiyendeProcessHelper
import com.tiyende.kash.loop.utils.track.TrackPointManager
import org.json.JSONObject
import java.math.BigDecimal

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/12/16 13:41
 * @Annotation：额度审核完成
 */
class TiyendeLimitCompleteActivity: CommonActivity(),IViewContract.ITiyendeLimitReviewingView {
    lateinit var binding: TiyendeActivityLimitCompleteBinding
    private var presenter: TiyendeLimitPresenter? = null

    override fun getTiyendeRootView(): View? {
        binding = TiyendeActivityLimitCompleteBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initTiyendeView() {
        setTiyendeTitleBarVisibility(View.VISIBLE)
        setTiyendeTextTitleVisibility(View.GONE)
        setTiyendeBackBtnRes(R.mipmap.tiyende_back_white)
        setTiyendeRightBtnRes(R.mipmap.tiyende_customer_white)
        setTiyendeSwitchLangBtnRes(R.mipmap.tiyende_language_icon)
        eventRemark = "Limit_Complete_Page"
    }

    override fun initTiyendeListener() {
        super.initTiyendeListener()
        binding.btnWithdrawNow.setOnClickListener {
            addEventPointRequestAction(TiyendeEventPointType.CLICK.TIYENDE_CLICK_LIMIT_COMPLETE_WITHDRAW_NOW,eventRemark)
            // TODO: 额度审核 Withdraw Now 上报所有埋点
            TrackPointManager.getInstance().manualReport()

            presenter?.checkTiyendeLimitReviewingPermissionsAgree()
        }
        binding.btGoHomePage.setOnClickListener {
            finish()
        }
    }

    override fun loadTiyendeData() {
        if (presenter == null){
            presenter = TiyendeLimitPresenter(this)
        }
        presenter?.queryTiyendeCreditStatusAction()
    }

    /**
     * 额度审核中
     */
    @SuppressLint("SetTextI18n")
    override fun showTiyendeLimitReviewingViews() {
        onIntent(TiyendeLimitReviewingActivity::class.java)
        finish()
    }

    /**
     * 额度审核成功
     */
    @SuppressLint("SetTextI18n")
    override fun showTiyendeLimitSuccessViews(data: JSONObject) {
        val availableAmount = BigDecimal(data.optString("availableAmount")).stripTrailingZeros().toPlainString()
        val minAmount = BigDecimal(data.optString("minAmount")).stripTrailingZeros().toPlainString()
        val maxAmount = BigDecimal(data.optString("maxAmount")).stripTrailingZeros().toPlainString()

        binding.txtExpirse.text = " : ${TiyendeProcessHelper.queryTiyendeAuthenticationCreditExpireDate()}"
        binding.txtAmountUnit.text = data.optString("currency")
        binding.textLimitReviewingAmount.text = availableAmount
    }

    /**
     * 额度审核失败
     */
    override fun showTiyendeLimitFailedViews() {
        onIntent(TiyendeLimitRejectedActivity::class.java)
        finish()
    }

    /**
     * 申贷
     */
    override fun showTiyendeLimitApplyPage() {
        TiyendeLoanApplyActivity.start(this)
        finish()
    }

    override fun showTiyendeLimitPermissionPage() {
        onIntent(TiyendePermissionListActivity::class.java)
        finish()
    }

    override fun onTiyendePublishEvent(event: TiyendeEventData) {
        super.onTiyendePublishEvent(event)
        if (event.eventType == TiyendeEventData.TYPE.TIYENDE_PROCESS_REFRESHED){
            presenter?.queryTiyendeCreditStatusAction()
        }
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_CREDIT_COMPLETE_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_CREDIT_COMPLETE_END)
    }
}